/**
 * 
 */
package tp8.tsh.commands.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tp8.tsh.cli.CurrentDirectory;
import tp8.tsh.cli.TSHOutput;
import tp8.tsh.commands.intf.Command;
import tp8.tsh.exc.TSHException;

/**
 * @author tpana
 * 
 */
public class ChangeDir implements Command {

	@SuppressWarnings("unused")
	private TSHOutput out;

	/*
	 * (non-Javadoc)
	 * 
	 * @see tp8.tsh.commands.intf.Command#setTSHOutput(tp8.tsh.cli.TSHOutput)
	 */
	@Override
	public void setTSHOutput(TSHOutput tshOutput) {
		this.out = tshOutput;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tp8.tsh.commands.intf.Command#getName()
	 */
	@Override
	public String getName() {
		return "cd";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tp8.tsh.commands.intf.Command#getVersion()
	 */
	@Override
	public String getVersion() {
		return "1.0";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tp8.tsh.commands.intf.Command#getAliasMap()
	 */
	@Override
	public Map<String, String> getAliasMap() {
		Map<String, String> aliasMap = new HashMap<String, String>();
		aliasMap.put("cd", null);
		return aliasMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tp8.tsh.commands.intf.Command#getArgsRegEx()
	 */
	@Override
	public List<String> getArgsRegEx() {
		List<String> argsList = new ArrayList<String>();
		argsList.add("[0-9a-zA-Z\\.\\\\/]*");
		return argsList;
	}

	@Override
	public void execute(String[] args, CurrentDirectory curDir,
			Map<String, String> envVars) throws TSHException {
		String newPath = args[0];

		if ((newPath.startsWith("."))
				|| ((!newPath.startsWith("\\")) && (!newPath.startsWith("/")))) {
			// If relative path
			curDir.changeRelativePath(newPath);
		} else {
			// If absolute path
			curDir.changePath(newPath);
		}

	}

}
